package src;

import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import java.util.Vector;
import util.CommandReceiver;

/**
 *
 * @author TJ
 */
public class Receiver extends CommandReceiver {
    public static final String COMMAND_PREFIX = "SuperDash_Command_";
    public static final int NUM_COMMAND_SLOTS = 10;

    private Vector recievedCommands = new Vector();

    private void update() {
        for(int a = 0; a < NUM_COMMAND_SLOTS; a++) {
            String field = SmartDashboard.getString(COMMAND_PREFIX + a, null);
            if(field == null) {
                SmartDashboard.putString(COMMAND_PREFIX + a, "");
            } else if(field.length() > 0) {
                String name = field.substring(0, field.indexOf(" "));
                String arguments = field.substring(field.indexOf(" ") + 1);
                Vector argumentList = new Vector();
                while(arguments.length() > 0) {
                    String argument = arguments.substring(0, field.indexOf(" "));
                    argumentList.addElement(argument);
                    arguments = arguments.substring(field.indexOf(" ") + 1);
                }
                recievedCommands.addElement(new Command(name, argumentList));
            }
            SmartDashboard.putString(COMMAND_PREFIX + a, "");
        }
    }

    public boolean isCommandAvailable() {
        update();
        return !recievedCommands.isEmpty();
    }

    public Command pollCommand() {
        update();
        if(recievedCommands.isEmpty()) return null;
        Command out = (Command) recievedCommands.elementAt(0);
        recievedCommands.removeElementAt(0);
        return out;
    }

}
